﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpeakOut.Data;
using SpeakOut.Lib;
using SpeakOut.Data.Interfaces;
using SpeakOut.Model;
using log4net;

namespace SpeakOut.Service
{
    public class AdminService : IAdminService
    {
        private static readonly ILog _log = LogManager.GetLogger(typeof(AdminService));

        private readonly IPostDao _postDao;
        private readonly ICategoryDao _categoryDao;
        private readonly IAuthorDao _authorDao;

        public AdminService(IPostDao postDao, ICategoryDao categoryDao, IAuthorDao authorDao)
        {
            _postDao = postDao;
            _categoryDao = categoryDao;
            _authorDao = authorDao;
        }

        public IList<Post> GetPosts()
        {
            return _postDao.FindAll();
        }

        public Post GetPost(Guid postId)
        {
            return _postDao.FindById(postId);
        }

        public IList<Category> GetCategories()
        {
            return _categoryDao.FindAll();
        }

        public IList<Category> GetCategories(IEnumerable<Guid> ids)
        {
            return _categoryDao.FindAll().Where(x => ids.Contains(x.CategoryId)).ToList();
        }

        public Author GetAuthor(string login)
        {
            return _authorDao.FindByLogin(login);
        }

        public Author GetAuthor(Guid authorId)
        {
            return _authorDao.FindById(authorId);
        }

        public void SavePost(Post post)
        {
            _postDao.MakePersistent(post);
        }

        public void SaveCategory(Category category)
        {
            _categoryDao.MakePersistent(category);
        }
    }
}
